package com.example.shixun1.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.shixun1.R;

// AddArticleActivity.java
public class AddArticleActivity extends AppCompatActivity {
    private EditText etTitle, etContent, etAuthor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_article);

        etTitle = findViewById(R.id.et_title);
        etContent = findViewById(R.id.et_content);
        etAuthor = findViewById(R.id.et_author);

        findViewById(R.id.btn_submit).setOnClickListener(v -> {
            String title = etTitle.getText().toString();
            String content = etContent.getText().toString();
            String author = etAuthor.getText().toString();

            if (title.isEmpty() || content.isEmpty()) {
                Toast.makeText(this, "标题和内容不能为空", Toast.LENGTH_SHORT).show();
                return;
            }

            // 生成新文章ID
            String articleId = "article_" + System.currentTimeMillis();

            // 保存到SharedPreferences
            SharedPreferences.Editor editor = getSharedPreferences("articles", MODE_PRIVATE).edit();
            editor.putString(articleId + "_title", title);
            editor.putString(articleId + "_content", content);
            editor.putString(articleId + "_author", author);
            editor.putLong(articleId + "_time", System.currentTimeMillis());
            editor.apply();

            Toast.makeText(this, "文章添加成功", Toast.LENGTH_SHORT).show();
            // 设置结果通知刷新
            setResult(RESULT_OK);
            finish();
        });
    }
}
